import {random_move,get_valid_moves} from "/go/random_move.js"
import {get_neighbors_4,get_neighbors_8} from "/go/get_neighbors.js"
import {score_of_step,score_of_step_list} from "/go/score_of_step.js"
import {first_step} from "/go/first_step.js"

/** @param {NS} ns */
export async function main(ns) {
  //ns.go.cheat.playTwoMoves(1,1,2,2);
  
  var chains = ns.go.analysis.getChains();
  //ns.tprint(chains);
  // ns.tprint("生成的链路:");
  // for(let i=0;i<chains.length;++i)
  // {
  //   // 从每列的最后一位开始输出
  //   let strLine = "";
  //   strLine+='\t';
  //   // 这里的j用于列, 从左到右依次遍历列
  //   for(let j=0;j<chains.length;++j)
  //   {
  //     var column = chains[j];
  //     var numberID = column[chains.length-1-i];
  //     strLine+=numberID;
  //     strLine+='\t';
  //   }
  //   ns.tprint("",strLine);
  //   ns.tprint("");
  //   // for(let i=chains.length-1;i>=0;--i)
  //   // {
  //   // }
  // }

  // // ----    以上是 getChains    ----

  var game_state = ns.go.getGameState();
  ns.tprint("游戏状态：", game_state);
  if(null==game_state.previousMove)
  {
    ns.tprint("\t\t游戏为结束状态");
  }
  if("None"==game_state["currentPlayer"])
  {
    ns.tprint("\t\t游戏为结束状态");
  }
  var board_state = ns.go.getBoardState();
  if(await first_step(board_state,ns))
  {
    board_state = ns.go.getBoardState();
    ns.tprint("走了第一步");
  }
  ns.tprint("board_state:", board_state);
  for(let i=0;i<board_state.length;++i)
  {
    // 从每列的最后一位开始输出
    let strLine = "";
    strLine+='  ';
    // 这里的j用于列, 从左到右依次遍历列
    for(let j=0;j<board_state.length;++j)
    {
      var column = board_state[j];
      var str_who = column[board_state.length-1-i];
      strLine+=str_who;
      strLine+='  ';
    }// END for
    ns.tprint("",strLine);
  }
  // 落子:
  // ns.go.makeMove(2,1);

  var promis_next = await ns.go.opponentNextTurn();
  ns.tprint("promis_next:",promis_next);

  // var opponent_name = ns.go.getOpponent();
  // ns.tprint("对手:",opponent_name);

  // // 重启游戏 指定对手为"Netburners",指定5X5:
  // ns.go.resetBoardState("Netburners",5);
  
  const valid_moves = ns.go.analysis.getValidMoves();
  // ns.tprint("valid_moves:",valid_moves);
  // for(let i=0;i<valid_moves.length;++i)
  // {
  //   // 从每列的最后一位开始输出
  //   let strLine = "";
  //   strLine+='\t';
  //   // 这里的j用于列, 从左到右依次遍历列
  //   for(let j=0;j<valid_moves.length;++j)
  //   {
  //     var column = valid_moves[j];
  //     var str_who = column[valid_moves.length-1-i];
  //     strLine+=str_who;
  //     strLine+='\t';
  //   }// END for
  //   ns.tprint("",strLine);
  //   ns.tprint("");
  // }

  
  // ns.tprint("与各对手的对战情况,以及得到的好处");
  // var analysis_stats = ns.go.analysis.getStats();
  // ns.tprint("analysis_stats:",analysis_stats);

  ns.tprint("liberties包围情况:");
  var liberties = ns.go.analysis.getLiberties();
  // ns.tprint(liberties);
  for(let i=0;i<liberties.length;++i)
  {
    // 从每列的最后一位开始输出
    let strLine = "";
    strLine+='\t';
    // 这里的j用于列, 从左到右依次遍历列
    for(let j=0;j<liberties.length;++j)
    {
      var column = liberties[j];
      var str_who = column[liberties.length-1-i];
      strLine+=str_who;
      strLine+='\t';
    }// END for
    ns.tprint("",strLine);
    ns.tprint("");
  }

  // ns.tprint("随机的移动:",random_move(board_state,valid_moves,ns));
  var valid_moves_list = get_valid_moves(valid_moves);
  // ns.tprint("取所有的可移动点:",valid_moves_list);
  

  var a_move = random_move(board_state,valid_moves,ns);
  if(0==a_move.length)
  {
    ns.tprint("!!!!没步可走1");
  }
  var neighbors = get_neighbors_8(board_state,a_move[0],a_move[1]);
  var is_has_neighbor = false;//neighbors.includes("X");
  var time_limit = 1000;
  while(!is_has_neighbor && --time_limit)
  {
    neighbors = get_neighbors_8(board_state,a_move[0],a_move[1]);
    is_has_neighbor = neighbors.includes("X");
    if(is_has_neighbor)
    {
      ns.tprint("随机步可行:",a_move);
      break;
    }
    else
    {
      a_move = random_move(board_state,valid_moves,ns);
    }
  }
  if(time_limit<=0)
  {
    ns.tprint("!!!!没步可走2");
  }
  else
  {
    ns.tprint("time_limit:",time_limit);
  }


  var score_list = score_of_step_list(board_state,valid_moves_list,chains,ns);
  ns.tprint("取到分数表:" );
  for(var i=0;i<score_list.length;++i)
  {
    ns.tprint("\t",score_list[i].x,",",score_list[i].y,"\t",score_list[i].score);
  }

  // 在分数表中随机一步:
  if(score_list.length)
  {
    const max_score = score_list[0].score;
    var size_of_max_score = 0;
    for(var i=0;i<score_list.length;++i)
    {
      if(max_score == score_list[i].score)
      {
        size_of_max_score ++;
      }
      else
      {
        break;
      }
    }
    var indexRandom = Math.floor(Math.random() * size_of_max_score);
    ns.tprint("随机:",indexRandom,"\t高有分有几个:",size_of_max_score);
    ns.tprint("随机到的步:",score_list[indexRandom].x,",",score_list[indexRandom].y);
  }

}